package pers.dawnyang.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 
 * @author dawn
 * @date 2020/12/14
 */
@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext appCtx;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.appCtx = applicationContext;
    }

    /**
     * 
     * @return
     */
    public static ApplicationContext getAppCtx() {
        return appCtx;
    }

    /**
     * 
     * @param name
     * @return
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return appCtx.getBean(name);
    }

    /**
     * 
     * @param requiredType
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> requiredType) throws BeansException {
        return appCtx.getBean(requiredType);
    }

}
